package com.cheli.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheli.commonutils.R;
import com.cheli.manager.client.RoleClient;
import com.cheli.manager.entity.ManaStaff;
import com.cheli.manager.entity.vo.StaffQuery;
import com.cheli.manager.service.ManaStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工 前端控制器
 * </p>
 *
 * @author cheli
 * @since 2021-03-14
 */
@RestController
@RequestMapping("/manager/staff")
@Api(value = "staff", tags = "员工管理")
@CrossOrigin
@Slf4j
public class ManaStaffController {
    private final ManaStaffService staffService;

    @Autowired
    public ManaStaffController(ManaStaffService staffService) {
        this.staffService = staffService;
    }

    @Autowired
    private RoleClient roleClient;

   @ApiOperation("所有员工列表")
    @GetMapping("findAll")
    public R findAllStaff(){
        log.info("查询所有员工");

        List<ManaStaff> list = staffService.list(null);

        return R.ok().data("items", list);
    }

    @ApiOperation("逻辑删除员工")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id) {
        boolean flag = staffService.removeById(id);
        log.info("删除员工"+id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("员工分页查询")
    @GetMapping("pageStaff/{current}/{limit}")
    public R pageListStaff(@PathVariable long current,
                              @PathVariable long limit) {
        log.info("员工分页查询");
       Page<ManaStaff> page = new Page<>(current, limit);
        staffService.page(page, null);

        long total = page.getTotal();
        List<ManaStaff> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("员工条件分页查询")
    @PostMapping("pageStaffCondition/{current}/{limit}")
   public R pageStaffCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) StaffQuery StaffQuery) {
        log.info("员工分页条件查询" + StaffQuery.toString());
       Page<ManaStaff> page = new Page<>(current, limit);
        QueryWrapper<ManaStaff> wrapper = new QueryWrapper<>();


        String begin = StaffQuery.getBegin();
        String end = StaffQuery.getEnd();
        String name = StaffQuery.getName();
        String level = StaffQuery.getLevel();


        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        wrapper.orderByAsc("sort");

        staffService.page(page, wrapper);
        long total = page.getTotal();
        List<ManaStaff> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }


   @ApiOperation("添加员工")
    @PostMapping("addStaff")
    public R addStaff(@RequestBody ManaStaff manaStaff) {
        log.info("添加员工" + manaStaff.getId());
        boolean save = staffService.save(manaStaff);
        roleClient.setRoleByUserId(manaStaff.getId(), "3");
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

   @GetMapping("getStaff/{id}")
    @ApiOperation("根据ID查询员工")
    public R getStaffById(@PathVariable String id) {
        log.info("查询员工" + id);
        ManaStaff manaStaff = staffService.getById(id);
        return R.ok().data("staff", manaStaff);
    }

    @PostMapping("updateStaff")
    @ApiOperation("更改员工")
    public R updateStaff(@RequestBody ManaStaff manaStaff) {
        log.info("更改员工" + manaStaff.getId());
        boolean update = staffService.updateById(manaStaff);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

